﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Mang24k99.Model.Dissertation;
using Mang24k99.Business.Dissertation;
using System.Collections.Generic;

namespace Mang24k99.Web.Dissertation
{
    public partial class List : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ViewState["OnlineUserNumber"] = "007"; 
            ViewState["DissertationPageIndexNow"] = "1";
            if (!IsPostBack)
            {
                try
                {
                    selectWeb.SelectedValue = Request["WEBID"].ToString();
                    ViewState["DissertationPageIndexNow"] = int.Parse(Request["PageIndex"].ToString());
                }
                catch { ViewState["DissertationPageIndexNow"] = "1"; }

                int pageNow = int.Parse(ViewState["DissertationPageIndexNow"].ToString());
                if (pageNow != 1)
                    BindManageSpecial(NewsPager.PageSize*(pageNow-1),NewsPager.PageSize*pageNow);
                else
                    BindManageSpecial(0, NewsPager.PageSize);
            }
        }

        private void BindManageSpecial(int iStart,int iEnd)
        {
            string webID = selectWeb.SelectedValue;
            int iCount = 0;
            ManageSpecial.DataSource = BDissertations.FindAllList(webID,iStart,iEnd,out iCount,null);
            
            NewsPager.RecordCount = iCount;
            if (ViewState["DissertationPageIndexNow"].ToString() != "1")
            {
                NewsPager.CurrentPageIndex = int.Parse(ViewState["DissertationPageIndexNow"].ToString());
                ViewState["DissertationPageIndexNow"] = 1;
            }

            if (iStart == 0)
                NewsPager.CurrentPageIndex = 1;
            ManageSpecial.DataKeyNames = new string[] { "DisID" };
            ManageSpecial.DataBind();
        }

        protected void selectWeb_SelectedIndexChanged(object sender, EventArgs e)
        {
            SpecialWeb.Text = selectWeb.SelectedItem.Text;
            BindManageSpecial(0, NewsPager.PageSize );
        }

        protected void ManageSpecial_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if(e.Row.RowType==DataControlRowType.DataRow)
            {
                if(e.Row.RowState==DataControlRowState.Alternate||e.Row.RowState==DataControlRowState.Normal)
                {
                    //if (e.Row.Cells[1].Text.Length > 20)
                    //    e.Row.Cells[1].Text = e.Row.Cells[1].Text.Substring(0, 20) + "...";
                    DataRowView drv = (DataRowView)e.Row.DataItem;
                    if (int.Parse(drv[2].ToString())<26)
                        e.Row.Cells[2].Text = GetDisTitle(drv[2].ToString());
                    else
                        e.Row.Cells[2].Text = "无";
                    if (drv[3].ToString() == "1")
                        e.Row.Cells[6].Text = "已上线";
                    else
                        ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('您确认要上线专题:\"" + e.Row.Cells[1].Text + "\"吗?');");
                    ((LinkButton)e.Row.Cells[8].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('您确认要删除专题:\"" + e.Row.Cells[1].Text + "\"吗?');");
                }
            }
        }

        protected void Search_Click(object sender, EventArgs e)
        {
            BindSearch(0, NewsPager.PageSize );
        }

        private void BindSearch(int iStart,int iEnd){
            string webID = selectWeb.SelectedValue;
            string filter = "1=1 ";
            if (!string.IsNullOrEmpty(SpecialName.Text))
                filter += " and Name like '%" + SpecialName.Text + "%'";
            if (!string.IsNullOrEmpty(startTime.Text))
                filter += " and CreateTime>='" + Convert.ToDateTime(startTime.Text)+"'";//.Replace("-","");
            else
                filter += " and CreateTime>='" + new DateTime(1900,12,12) + "'";
            if (!string.IsNullOrEmpty(endTime.Text))
                filter += " and CreateTime<='" + Convert.ToDateTime(endTime.Text).AddDays(1) + "'";//.Replace("-", "");
            else
                filter += " and CreateTime<='" + DateTime.Now.AddDays(1) + "'";
            if (!string.IsNullOrEmpty(startTime.Text) && !string.IsNullOrEmpty(endTime.Text) && Convert.ToDateTime(startTime.Text) > Convert.ToDateTime(endTime.Text))
                Common.MessageBox.Show(this, "请输入正确的查询条件!");
            else
            {
                int iCount = 0;
                DataTable newTd = BDissertations.FindAllList(webID, iStart, iEnd, out iCount, filter);
                NewsPager.RecordCount = iCount;

                ManageSpecial.DataSource = newTd;
                ManageSpecial.DataKeyNames = new string[] { "DisID" };
                ManageSpecial.DataBind();
            }
        }

        /// <summary>
        /// 添加专题
        /// </summary>
        protected void AddSpecial_Click(object sender, EventArgs e)
        {
            Response.Redirect("Edit.aspx?WEBID="+selectWeb.SelectedValue);
        }

        /// <summary>
        /// 设置专题顺序
        /// </summary>
        protected void SettingSort_Click(object sender, EventArgs e)
        {
            Response.Redirect("Order.aspx?WebID="+selectWeb.SelectedValue);
        }

        protected void ManageSpecial_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                string index = ManageSpecial.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString();
                MDissertations mdis = MDissertations.Find(int.Parse(index));
                if (e.CommandName == "deletedis")
                {
                    mdis.BDeleted = 0;
                    int number = mdis.DissertationNumber;
                    mdis.DissertationNumber = 26;
                    mdis.DissertationOnline = 0;
                    mdis.LastUpdaterID = int.Parse(ViewState["OnlineUserNumber"].ToString());
                    mdis.Delete();//删除专题

                    //让后面的专题上来
                    BDissertations.SetLastDisdertation(number);

                    if (NewsPager.EndRecordIndex == NewsPager.StartRecordIndex)//最后一页
                    {
                        if(NewsPager.StartRecordIndex!=1)//不是第一条记录
                            BindManageSpecial(NewsPager.StartRecordIndex - NewsPager.PageSize - 1, NewsPager.EndRecordIndex - NewsPager.PageSize + 1);
                    }
                    else
                        BindManageSpecial(NewsPager.StartRecordIndex - 1, NewsPager.EndRecordIndex );


                    ////if ((NewsPager.EndRecordIndex -1)% NewsPager.PageSize == 0)
                    //if(NewsPager.CurrentPageIndex==NewsPager.PageCount)//最后一页
                    //{
                    //    if (NewsPager.CurrentPageIndex == 1)
                    //        BindManageSpecial(0, NewsPager.RecordCount - 1);
                    //}
                    //else//前面的页 if(NewsPager.CurrentPageIndex<NewsPager.PageCount)
                    //{

                    //}


                    //if (NewsPager.EndRecordIndex == NewsPager.StartRecordIndex )
                    //{
                    //    if (NewsPager.EndRecordIndex==1)
                    //        BindManageSpecial(NewsPager.StartRecordIndex- 1,1);
                    //    else
                    //        BindManageSpecial(NewsPager.StartRecordIndex - NewsPager.PageSize - 1, NewsPager.EndRecordIndex - NewsPager.PageSize + 1);
                    //}
                    //else
                    //{
                    //    if (NewsPager.EndRecordIndex < NewsPager.PageCount)
                    //        BindManageSpecial(NewsPager.StartRecordIndex - 1, NewsPager.EndRecordIndex );
                    //    else
                    //        BindManageSpecial(NewsPager.StartRecordIndex - 1, NewsPager.EndRecordIndex - 1);
                    //}
                }
                else if (e.CommandName == "setting")
                {
                    BDissertations.SetDisOnline(selectWeb.SelectedValue, mdis.DissertationID.ToString());//设置上线
                    BindManageSpecial(NewsPager.StartRecordIndex - 1, NewsPager.EndRecordIndex);
                }
            }
            catch { }
        }

        /// <summary>
        /// 翻页绑定数据
        /// </summary>
        protected void NewsPager_PageChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(SpecialName.Text) || !string.IsNullOrEmpty(startTime.Text) || !string.IsNullOrEmpty(endTime.Text))
                BindSearch(NewsPager.StartRecordIndex - 1, NewsPager.EndRecordIndex);
            else
                BindManageSpecial(NewsPager.StartRecordIndex - 1, NewsPager.EndRecordIndex);
                
        }

        /// <summary>
        /// 标识专题
        /// </summary>
        /// <param name="id">专题位置</param>
        /// <returns></returns>
        private string GetDisTitle(string id)
        {
            string[] number = new string[] { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", 
                "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十", 
                "二十一", "二十二", "二十三", "二十四", "二十五" };
            return "专题" + number[int.Parse(id) - 1];
        }

        /// <summary>
        /// 子新闻管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void manageNews_Click(object sender, EventArgs e)
        {
            Response.Redirect("ChildNews/List.aspx?WebID=" + selectWeb.SelectedValue);//?webID="+selectWeb.SelectedValue);
        }

        /// <summary>
        /// 参考文件管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void disFileManage_Click(object sender, EventArgs e)
        {
            Response.Redirect("Reference/List.aspx?WEBID="+selectWeb.SelectedValue+"&PageIndex=1");
        }
    }
}